/*
    Clase que contiene acciones referentes al logeo de los trabajadores.
    //1 Este m�todo es el previo para el m�todo que viene a continuacion. Aqu�
    se hace la verificacion de trabajadores registrados 
    //2 Este m�todo es el que permite acceder al sistema al usuario que este
    registrado.
 */

package com.action.proceso4;

import com.action.proceso6.ContratoAction;
import com.bean.proceso4.Area;
import com.bean.proceso4.Trabajador;
import com.bean.proceso6.Contrato;
import com.conexion.DbConnection;
import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoginAction {

	// 1
	public Object[][] select(String table, String fields, String where) {
		Connection cn = null;
		PreparedStatement pstm = null;
		int registros = 0;
		String colname[] = fields.split(",");
		String q = "SELECT " + fields + " FROM " + table;
		String q2 = "SELECT count(*) as total FROM " + table;
		if (where != null) {
			q += " WHERE " + where;
			q2 += " WHERE " + where;
		}
		try {
			cn = DbConnection.getConnection();
			pstm = cn.prepareStatement(q2);
			ResultSet rs = pstm.executeQuery();
			rs.next();
			registros = rs.getInt("total");
			rs.close();
		} catch (SQLException e) {
			System.out.println(e);
		}

		Object data[][] = new String[registros][fields.split(",").length];
		try {
			pstm = cn.prepareStatement(q);
			ResultSet rs = pstm.executeQuery();
			int i = 0;
			while (rs.next()) {
				for (int j = 0; j <= fields.split(",").length - 1; j++) {
					data[i][j] = rs.getString(colname[j].trim());
				}
				i++;
				rs.close();
			}
		} catch (SQLException e) {
			System.out.println(e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (cn != null)
					cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return data;
	}

	// 2
	public boolean Ingresar(String user, String pass) {
		Object[][] rs = this.select("trabajador", "username , password",
				"username='" + user + "' " + "AND password='" + pass + "' ");
		if (rs.length > 0) {
			Trabajador tra = new Trabajador();
			tra.setUsername(rs[0][0].toString());
			tra.setPassword(rs[0][1].toString());
			return true;
		} else {
			return false;
		}
	}

	public Area Logeo(String username, String password) {
		Connection conn = null;
		PreparedStatement pstm = null;
		Area a = null;
		try {
			conn = DbConnection.getConnection();

			String sql = "Select a.* FROM trabajador t inner join area_trabajador at on t.codigo=at.trabajador_id inner join area a on at.area_id=a.codigo  where t.username=? and t.password=?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, password);

			ResultSet rs = pstm.executeQuery();

			if (rs.next()) {
				a = new Area(rs.getString(1), rs.getString(2));
			}

		} catch (SQLException e) {
			Logger.getLogger(ContratoAction.class.getName()).log(Level.SEVERE,
					null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return a;
	}

}
